<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <router-link to="/warehouse/receipt/add" class="mainor_tea"
              >创建入库单</router-link
            >
            <!-- <el-popconfirm
              title="您确定导出当前的入库单号吗？"
              @onConfirm="export_de"
            >
              <el-button slot="reference">导出</el-button>
            </el-popconfirm> -->
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="入库状态" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="入库状态"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(statusList, index) in list.statusList"
                  :key="index"
                  :label="statusList"
                  :value="parseInt(index + 1)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="入库类型" prop="type">
              <el-select
                v-model.trim="form.type"
                placeholder="入库类型"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(typeList, index) in list.typeList"
                  :key="index"
                  :label="typeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-if="moredata">
                <el-form-item label="入库单号" prop="order_sn">
                  <el-input
                    v-model.trim="form.order_sn"
                    placeholder="入库单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="关联单号" prop="related_order_sn">
                  <el-input
                    v-model.trim="form.related_order_sn"
                    placeholder="关联单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="采购方式" prop="mode">
                  <el-select
                    v-model.trim="form.mode"
                    placeholder="采购方式"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(modeList, index) in list.modeList"
                      :key="index"
                      :label="modeList"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="采购员" prop="buyer_id">
                  <el-select
                    v-model.trim="form.buyer_id"
                    placeholder="采购员"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(buyer, index) in list.buyer"
                      :key="index"
                      :label="buyer"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="供应商" prop="supplier_id">
                  <el-select
                    v-model.trim="form.supplier_id"
                    placeholder="供应商"
                    class="wid220"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(supplier, index) in list.supplier"
                      :key="index"
                      :label="supplier"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="创建日期" prop="establishTime">
                  <el-date-picker
                    v-model="establishTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                  />
                </el-form-item>
                <el-form-item label="入库时间" prop="createTime">
                  <el-date-picker
                    v-model="createTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                  />
                </el-form-item>
                <div class="clearfix"></div>
              </div>
            </el-collapse-transition>
            <el-form-item class="marginTop10">
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="
                  $refs['userListFrom'].resetFields(),
                    (establishTime = []),
                    (createTime = [])
                "
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column type="selection" />
            <el-table-column label="入库单号" prop="order_sn" />

            <el-table-column label="入库类型">
              <template slot-scope="scope">{{
                list.typeList[scope.row.type]
              }}</template>
            </el-table-column>
            <el-table-column label="关联单号" prop="related_order_sn" />
            <el-table-column label="入库金额" prop="money" />
            <el-table-column label="入库状态" prop="status">
              <template slot-scope="scope"
                ><span :class="'order' + scope.row.status">
                  {{ list.statusList[scope.row.status] }}</span
                ></template
              >
            </el-table-column>
            <el-table-column label="创建时间" prop="created_at">
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>
            <el-table-column label="入库时间" prop="updated_at">
              <template slot-scope="scope">{{
                scope.row.storage_time
                  ? parseTime(scope.row.storage_time, "{y}-{m}-{d}")
                  : ""
              }}</template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="left"
              fixed="right"
              width="180"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <router-link :to="'/warehouse/receipt/see/' + scope.row.id">
                  <el-link type="success">查看</el-link>
                </router-link>
                <router-link :to="'/warehouse/receipt/edit/' + scope.row.id">
                  <el-link type="warning" v-if="scope.row.status == 0"
                    >审核</el-link
                  >
                </router-link>
                <el-link
                  style="color: rgb(17, 138, 178)"
                  @click="exportOrderiInfo(scope.row.id)"
                  >导出</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  storage_index,
  export_storage_info,
} from "@/api/warehouse/warehouseReceipt";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        status: null,
        type: null,
        order_sn: "",
        related_order_sn: "",
        storage_start_time: "",
        storage_end_time: "",
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
      },
      createTime: [],
      establishTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      // 全选
      selectArr: [],
    };
  },
  created() {
    console.log(this.$route.params);
    if (this.$route.params.status) {
      this.form.status = parseInt(this.$route.params.status);
    }
    // 列表
    this.storage_index();
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
      console.log(this.selectArr);
    },
    // 导出明细
    export_de() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      console.log("你好");
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.storage_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.storage_index();
    },
    changePage(page) {
      this.current.page = page;
      this.storage_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    storage_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      storage_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      form.storage_start_time = this.establishTime[0]
        ? this.establishTime[0]
        : "";
      form.storage_end_time = this.establishTime[1]
        ? this.establishTime[1]
        : "";

      return form;
    },
    exportOrderiInfo(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      export_storage_info(data)
        .then((res) => {
          const link = document.createElement("a");
          // let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let blob = new Blob([res], {
            type:
              "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "入库管理单";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: $order_color1;
}
.order1 {
  color: $order_color2;
}
.order2 {
  color: $order_color4;
}
</style>
